package com.share.jdbc.main;

import com.share.common.repository.UserRepository;
import com.share.common.service.BaseService;
import com.share.common.type.ShardingType;
import com.share.jdbc.factory.YamlDataSourceFactory;
import com.share.jdbc.repository.UserRepositoryImpl;
import com.share.jdbc.service.OrderServiceImpl;
import com.share.jdbc.service.UserServiceImpl;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @desc:
 * @author:caifan
 * @date:2020/11/22
 */
public class YamlRawMain {

    private static final ShardingType shardingTables = ShardingType.SHARDING_TABLES;

    private static final ShardingType shardingDataBaseAndTables = ShardingType.SHARDING_DATABASES_AND_TABLES;

    private static final ShardingType shardingDataBases = ShardingType.SHARDING_DATABASES;

    public static void main(String[] args) throws Exception {
        DataSource dataSource = YamlDataSourceFactory.newInstance(shardingTables);
        userExecute(dataSource);
        orderExecute(dataSource);
    }

    /**
     * 配置了分表
     */
    private static void orderExecute(DataSource dataSource) throws SQLException {
        //service层
        BaseService orderService = new OrderServiceImpl(dataSource);
        /*
         * 创建order order_item address表并清空表
         */
        orderService.initEnvironment();
        /*
         * 插入数据并打印数据和删除数据
         */
        orderService.processSuccess();
        /*
         * 删除表
         */
        //orderService.cleanEnvironment();
    }

    /**
     * user表相关 分表插件未配置分表
     * @param dataSource
     * @throws SQLException
     */
    private static void userExecute(DataSource dataSource) throws SQLException {
        //dao层
        UserRepository userRepository = new UserRepositoryImpl(dataSource);
        //service层
        BaseService userService = new UserServiceImpl(userRepository);
        userService.initEnvironment();
        userService.processSuccess();
        userService.printData();
    }

}
